package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopcartMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShopcartServiceImpl {
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Autowired
    private ShopcartMapper shopcartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        java.util.List<ShoppingCart> list =shopcartMapper.list(shoppingCart);
        if(list != null && list.size() > 0){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shopcartMapper.update(cart);
        }
        else{
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId != null){
                Dish dish = dishMapper.getDIsh(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            else{
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getSetmealById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shopcartMapper.insert(shoppingCart);
        }
    }

    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart =  ShoppingCart.builder().userId(userId).build();
        return  shopcartMapper.list(shoppingCart);
    }
    //清空购物车
    public void clean() {
        shopcartMapper.delete(BaseContext.getCurrentId());
    }

    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //前端传来dishId或者setmealId
        Long dishId = shoppingCartDTO.getDishId();
        //两种情况菜品或者套餐数量只有1个，则直接删除，如果大于1个，则对数量进行
        //减去1，直到减到1后，在进行删除，
        if(dishId != null){
            ShoppingCart shoppingCart = shopcartMapper.getById(dishId);//查到特定的菜品或者套餐
            Integer number =shoppingCart.getNumber();
            if(number>1){
                shoppingCart.setNumber(number-1);
                shopcartMapper.update(shoppingCart);
            }else{
                shopcartMapper.deleteDishId(dishId);
            }
        }
        else{
            Long setmealId = shoppingCartDTO.getSetmealId();
            ShoppingCart shoppingCart = shopcartMapper.getById(setmealId);
            Integer number =shoppingCart.getNumber();
            if(number>1){
                shoppingCart.setNumber(number-1);
                shopcartMapper.update(shoppingCart);
            }else{
                shopcartMapper.deleteSetmealId(setmealId);
            }
        }
        //分别对菜品和套餐进行处理
    }
}
